package com.weipai.education.controller;

import com.weipai.education.model.InventoryIn;
import com.weipai.education.model.Purchase;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.PurchaseService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/13 10:01
 */
@RestController
@Validated
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    /**
     * 采购单分页查询
     *
     * @param page
     * @param size
     * @param startTime
     * @param endTime
     * @param key
     * @param type:不为null表示查询自己的进货单， 为空表示查询我下级发布的进货单
     * @return
     */
    @RequestMapping(value = "/purchase/list", method = RequestMethod.GET)
    public Response list(@NotNull(message = "page不能为空") @RequestParam(value = "page") Integer page,
                         @NotNull(message = "size不能为空") @RequestParam(value = "size") Integer size,
                         @RequestParam(value = "startTime", required = false) String startTime,
                         @RequestParam(value = "endTime", required = false) String endTime,
                         @RequestParam(value = "key", required = false) String key,
                         @RequestParam(value = "type", required = false) String type,
                         @RequestParam(value = "status", required = false) String status,
                         @NotBlank(message = "用户id不能为空") @RequestParam(value = "userId") String userId) {
        return purchaseService.list(page, size, startTime, endTime, key, type, userId, status);
    }

    /**
     * 市级加盟商查询他所在地区的区县、旗舰加盟商的拿货情况
     *
     * @param page
     * @param size
     * @param startTime
     * @param endTime
     * @param key
     * @return
     */
    @RequestMapping(value = "/purchase/cityLevelList", method = RequestMethod.GET)
    public Response cityLevelList(@NotNull(message = "page不能为空") @RequestParam(value = "page") Integer page,
                                  @NotNull(message = "size不能为空") @RequestParam(value = "size") Integer size,
                                  @RequestParam(value = "startTime", required = false) String startTime,
                                  @RequestParam(value = "endTime", required = false) String endTime,
                                  @RequestParam(value = "key", required = false) String key) {
        return purchaseService.cityLevelList(page, size, startTime, endTime, key);
    }

    /**
     * 进货单的保存
     *
     * @param purchase
     * @return
     */
    @RequestMapping(value = "/purchase/save", method = RequestMethod.POST)
    public Response save(@Valid Purchase purchase) {
        return purchaseService.save(purchase);
    }

    /**
     * 更改进货单的状态--出库
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/purchase/changeStatus", method = RequestMethod.GET)
    public Response changeStatus(@NotBlank(message = "进货单id不能为空") String id,
                                 String goodsBarCodes,
                                 @NotBlank(message = "产品id不能为空") String productId,
                                 @NotBlank(message = "快递公司不能为空") String dhl,
                                 @NotBlank(message = "快递单号不能为空") String expressNumber,
                                 Integer isHeadset,
                                 Integer productNumber) {
        return purchaseService.changeStatus(id, goodsBarCodes, productId, dhl, expressNumber,isHeadset,productNumber);
    }

    /**
     * 通过进货单号查询此进货单的商品条形码
     *
     * @param purchaseNumber
     * @return
     */
    @RequestMapping(value = "/purchase/getGoodsBarCodeByPurchaseNumber", method = RequestMethod.GET)
    public Response getGoodsBarCodeByPurchaseNumber(@NotBlank(message = "采购订单号不能为空") String purchaseNumber) {
        return purchaseService.getGoodsBarCodeByPurchaseNumber(purchaseNumber);
    }

    /**
     * 经销商入库
     *
     * @param inventoryIn
     * @return
     */
    @RequestMapping(value = "/purchase/inventoryInSave", method = RequestMethod.POST)
    public Response inventoryInSave(@Valid InventoryIn inventoryIn,
                                    @NotBlank(message = "商品条形码不能为空") String goodsBarCodes) {
        return purchaseService.inventoryInSave(inventoryIn, goodsBarCodes);
    }

    /**
     * 经销商无串码入库
     *
     * @param inventoryIn
     * @return
     */
    @RequestMapping(value = "/purchase/inventoryInSavebyNobarcode", method = RequestMethod.POST)
    public Response inventoryInSavebyNobarcode(InventoryIn inventoryIn,
                                               @Param("productNumber") Integer productNumber,
                                               @NotBlank(message = "产品名称不能为空") String productName,
                                               String inOrderNumber,
                                               String isheadset
    ) {
        return purchaseService.inventoryInSavebyNobarcode(inventoryIn, productNumber, productName,inOrderNumber,isheadset);
    }

    /**
     * 总部入库
     *
     * @param productId
     * @param goodsBarCodes
     * @param isPrice
     * @return
     */
    @RequestMapping(value = "/purchase/headquartersInventoryInSave", method = RequestMethod.POST)
    public Response headquartersInventoryInSave(@NotBlank(message = "产品id不能为空") String productId,
                                                String goodsBarCodes,
                                                @NotBlank(message = "是否为赠品不能为空") String isPrice,
                                                @NotBlank(message = "是否损坏不能为空") String isDamage,
                                                Integer productNumber,
                                                String spuNumber,
                                                @NotBlank(message = "产品名称不能为空") String name,
                                                Integer isHeadset) {
        return purchaseService.headquartersInventoryInSave(productId, goodsBarCodes, isPrice, isDamage, productNumber, spuNumber, name, isHeadset);
    }

    /**
     * 进货单详情
     *
     * @param purchaseNumber
     * @return
     */
    @RequestMapping(value = "/purchase/getPurchaseInfo", method = RequestMethod.GET)
    public Response getPurchaseInfo(@NotBlank(message = "采购订单号不能为空") String purchaseNumber) {
        return purchaseService.getPurchaseInfo(purchaseNumber);
    }

    /*
    * 经销商下单
    * */
    @RequestMapping(value="purchase/saveorder",method = RequestMethod.GET)
    public Response saveorder(@NotBlank(message = "产品id不能为空") String productId,
                                 Integer priductId,Purchase purchase){
        return purchaseService.saveOrder(productId,priductId,purchase);

    }

}
